<% title t("views.dashboard.heading") %>
<main id="main-content" class="crayons-layout crayons-layout--header-inside crayons-layout--2-cols mb-10">
  <%= render("header_and_action", page_title: t("views.dashboard.heading")) %>
  
  <div class="crayons-layout__content">
    <div class="flex items-center justify-between mt-3">
      <h2 class="hidden m:block crayons-subtitle-2"><%= t("views.dashboard.posts.heading") %></h2>

      <div class="flex flex-wrap px-3 gap-2">
        <% if params[:which] != "organization" %>
          <% if params[:state] == "status" %>
            <%= link_to t("views.dashboard.posts.full"), "/dashboard", class: "crayons-btn crayons-btn--outlined whitespace-nowrap" %>
          <% else %>
            <%= link_to t("views.dashboard.posts.statuses"), "/dashboard?state=status", class: "crayons-btn crayons-btn--outlined whitespace-nowrap" %>
          <% end %>
          <% if @articles.any?(&:archived) %>
            <%= link_to t("views.dashboard.posts.archived"), "javascript:;", id: "toggleArchivedLink", class: "crayons-btn crayons-btn--outlined whitespace-nowrap" %>
          <% end %>
          <% if @articles.any? %>
            <%= select_tag "dashboard_sort", options_for_select(sort_options, params[:sort]), "aria-label": t("views.dashboard.posts.option.aria_label"), class: "crayons-select w-auto" %>
          <% end %>
        <% end %>

        <% if @organizations && @organizations.size > 0 && (params[:which].blank? || params[:which] == "organization") %>
          <select id="dashboard_author" class="crayons-select w-auto">
            <option value="/dashboard" <%= "selected" if params[:which].blank? %>><%= t("views.dashboard.posts.personal") %></option>
            <% @organizations.each do |org| %>
              <option value="/dashboard/organization/<%= org.id %>" <%= "selected" if params[:org_id].to_i == org.id %>><%= t("views.dashboard.posts.org", name: org.name, num: org.articles_count) %></option>
            <% end %>
          </select>
        <% end %>
      </div>
    </div>

    <% if @articles.any? %>
      <%= javascript_include_tag "dashboardDropdowns", defer: true %>
      <div class="crayons-card mb-6 mt-3">
        <% @articles.each do |article| %>
          <% if params[:which] == "organization" %>
            <%= render "dashboard_article_row", article: article, organization: article.organization, org_admin: true, manage_view: false, article_series: article.collection.present? %>
          <% else %>
            <%= render "dashboard_article_row", article: article, organization: nil, org_admin: false, manage_view: false %>
          <% end %>
        <% end %>
      </div>

      <div class="mb-4 pl-3">
        <%= paginate @articles, params: { i: nil } %>
      </div>

    <% else %>
      <div class="p-6 m:p-9 crayons-card crayons-card--secondary align-center fs-l h-100 flex items-center justify-center flex-1 mt-4">
        <div>
          <% if params[:state] == "status" %>
            <p class="mb-6"><%= t("views.dashboard.posts.empty.status_desc") %></p>
          <% else %>
            <p class="mb-6"><%= t("views.dashboard.posts.empty.desc") %></p>
            <% if policy(Article).create? %>
              <p><a href="/new" class="crayons-btn crayons-btn--l"><%= t("views.dashboard.posts.empty.new") %></a></p>
            <% end %>
          <% end %>
        </div>
      </div>
    <% end %>
  </div>
</main>

<%= javascript_include_tag "archivedPostFilters", "dashboards/convertCoauthorIdsToUsernameInputs", defer: true %>
